package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuStatusService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@DubboService
public class MenuStatusServiceImpl implements MenuStatusService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        if (queryPageBean.getQueryString() != null) {
            criteria.orLike("name", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("linkUrl", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("description", "%" + queryPageBean.getQueryString() + "%");
        }
        Page<Menu> page = (Page<Menu>) menuMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Menu findById(Integer id) {
        Menu menu = menuMapper.selectByPrimaryKey(id);
        return menu;
    }

    @Override
    public void edit(Menu menu) {
        int i = menuMapper.updateByPrimaryKeySelective(menu);

    }

    @Override
    public void add(Menu menu) {
        menuMapper.insertSelective(menu);
    }


    @Override
    public void delete(Integer id) {
        Menu menu = new Menu();
        menu.setId(id);
        menuMapper.deleteByPrimaryKey(menu);
    }
}
